home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl / 5.10.0 / Pod / Man.pm < prev    next >
Encoding:
Perl POD Document  |  2009-06-26  |  66.1 KB  |  1,737 lines

  1. # Pod::Man -- Convert POD data to formatted *roff input.
  2. #
  3. # Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
  4. #     Russ Allbery <rra@stanford.edu>
  5. # Substantial contributions by Sean Burke <sburke@cpan.org>
  6. #
  7. # This program is free software; you may redistribute it and/or modify it
  8. # under the same terms as Perl itself.
  9. #
  10. # This module translates POD documentation into *roff markup using the man
  11. # macro set, and is intended for converting POD documents written as Unix
  12. # manual pages to manual pages that can be read by the man(1) command.  It is
  13. # a replacement for the pod2man command distributed with versions of Perl
  14. # prior to 5.6.
  15. #
  16. # Perl core hackers, please note that this module is also separately
  17. # maintained outside of the Perl core as part of the podlators.  Please send
  18. # me any patches at the address above in addition to sending them to the
  19. # standard Perl mailing lists.
  20.  
  21. ##############################################################################
  22. # Modules and declarations
  23. ##############################################################################
  24.  
  25. package Pod::Man;
  26.  
  27. require 5.005;
  28.  
  29. use strict;
  30. use subs qw(makespace);
  31. use vars qw(@ISA %ESCAPES $PREAMBLE $VERSION);
  32.  
  33. use Carp qw(croak);
  34. use Pod::Simple ();
  35. use POSIX qw(strftime);
  36.  
  37. @ISA = qw(Pod::Simple);
  38.  
  39. # Custom Debian version, see http://bugs.debian.org/500210
  40. $VERSION = '2.18_01';
  41. $VERSION = eval $VERSION;
  42.  
  43. # Set the debugging level.  If someone has inserted a debug function into this
  44. # class already, use that.  Otherwise, use any Pod::Simple debug function
  45. # that's defined, and failing that, define a debug level of 10.
  46. BEGIN {
  47.     my $parent = defined (&Pod::Simple::DEBUG) ? \&Pod::Simple::DEBUG : undef;
  48.     unless (defined &DEBUG) {
  49.         *DEBUG = $parent || sub () { 10 };
  50.     }
  51. }
  52.  
  53. # Import the ASCII constant from Pod::Simple.  This is true iff we're in an
  54. # ASCII-based universe (including such things as ISO 8859-1 and UTF-8), and is
  55. # generally only false for EBCDIC.
  56. BEGIN { *ASCII = \&Pod::Simple::ASCII }
  57.  
  58. # Pretty-print a data structure.  Only used for debugging.
  59. BEGIN { *pretty = \&Pod::Simple::pretty }
  60.  
  61. ##############################################################################
  62. # Object initialization
  63. ##############################################################################
  64.  
  65. # Initialize the object and set various Pod::Simple options that we need.
  66. # Here, we also process any additional options passed to the constructor or
  67. # set up defaults if none were given.  Note that all internal object keys are
  68. # in all-caps, reserving all lower-case object keys for Pod::Simple and user
  69. # arguments.
  70. sub new {
  71.     my $class = shift;
  72.     my $self = $class->SUPER::new;
  73.  
  74.     # Tell Pod::Simple not to handle S<> by automatically inserting  .
  75.     # Note that this messes up Unicode output by embedding explicit ISO 8859-1
  76.     # non-breaking spaces that we have to clean up later.
  77.     $self->nbsp_for_S (1);
  78.  
  79.     # Tell Pod::Simple to keep whitespace whenever possible.
  80.     if ($self->can ('preserve_whitespace')) {
  81.         $self->preserve_whitespace (1);
  82.     } else {
  83.         $self->fullstop_space_harden (1);
  84.     }
  85.  
  86.     # The =for and =begin targets that we accept.
  87.     $self->accept_targets (qw/man MAN roff ROFF/);
  88.  
  89.     # Ensure that contiguous blocks of code are merged together.  Otherwise,
  90.     # some of the guesswork heuristics don't work right.
  91.     $self->merge_text (1);
  92.  
  93.     # Pod::Simple doesn't do anything useful with our arguments, but we want
  94.     # to put them in our object as hash keys and values.  This could cause
  95.     # problems if we ever clash with Pod::Simple's own internal class
  96.     # variables.
  97.     %$self = (%$self, @_);
  98.  
  99.     # Initialize various other internal constants based on our arguments.
  100.     $self->init_fonts;
  101.     $self->init_quotes;
  102.     $self->init_page;
  103.  
  104.     # For right now, default to turning on all of the magic.
  105.     $$self{MAGIC_CPP}       = 1;
  106.     $$self{MAGIC_EMDASH}    = 1;
  107.     $$self{MAGIC_FUNC}      = 1;
  108.     $$self{MAGIC_MANREF}    = 1;
  109.     $$self{MAGIC_SMALLCAPS} = 1;
  110.     $$self{MAGIC_VARS}      = 1;
  111.  
  112.     return $self;
  113. }
  114.  
  115. # Translate a font string into an escape.
  116. sub toescape { (length ($_[0]) > 1 ? '\f(' : '\f') . $_[0] }
  117.  
  118. # Determine which fonts the user wishes to use and store them in the object.
  119. # Regular, italic, bold, and bold-italic are constants, but the fixed width
  120. # fonts may be set by the user.  Sets the internal hash key FONTS which is
  121. # used to map our internal font escapes to actual *roff sequences later.
  122. sub init_fonts {
  123.     my ($self) = @_;
  124.  
  125.     # Figure out the fixed-width font.  If user-supplied, make sure that they
  126.     # are the right length.
  127.     for (qw/fixed fixedbold fixeditalic fixedbolditalic/) {
  128.         my $font = $$self{$_};
  129.         if (defined ($font) && (length ($font) < 1 || length ($font) > 2)) {
  130.             croak qq(roff font should be 1 or 2 chars, not "$font");
  131.         }
  132.     }
  133.  
  134.     # Set the default fonts.  We can't be sure portably across different
  135.     # implementations what fixed bold-italic may be called (if it's even
  136.     # available), so default to just bold.
  137.     $$self{fixed}           ||= 'CW';
  138.     $$self{fixedbold}       ||= 'CB';
  139.     $$self{fixeditalic}     ||= 'CI';
  140.     $$self{fixedbolditalic} ||= 'CB';
  141.  
  142.     # Set up a table of font escapes.  First number is fixed-width, second is
  143.     # bold, third is italic.
  144.     $$self{FONTS} = { '000' => '\fR', '001' => '\fI',
  145.                       '010' => '\fB', '011' => '\f(BI',
  146.                       '100' => toescape ($$self{fixed}),
  147.                       '101' => toescape ($$self{fixeditalic}),
  148.                       '110' => toescape ($$self{fixedbold}),
  149.                       '111' => toescape ($$self{fixedbolditalic}) };
  150. }
  151.  
  152. # Initialize the quotes that we'll be using for C<> text.  This requires some
  153. # special handling, both to parse the user parameter if given and to make sure
  154. # that the quotes will be safe against *roff.  Sets the internal hash keys
  155. # LQUOTE and RQUOTE.
  156. sub init_quotes {
  157.     my ($self) = (@_);
  158.  
  159.     $$self{quotes} ||= '"';
  160.     if ($$self{quotes} eq 'none') {
  161.         $$self{LQUOTE} = $$self{RQUOTE} = '';
  162.     } elsif (length ($$self{quotes}) == 1) {
  163.         $$self{LQUOTE} = $$self{RQUOTE} = $$self{quotes};
  164.     } elsif ($$self{quotes} =~ /^(.)(.)$/
  165.              || $$self{quotes} =~ /^(..)(..)$/) {
  166.         $$self{LQUOTE} = $1;
  167.         $$self{RQUOTE} = $2;
  168.     } else {
  169.         croak(qq(Invalid quote specification "$$self{quotes}"))
  170.     }
  171.  
  172.     # Double the first quote; note that this should not be s///g as two double
  173.     # quotes is represented in *roff as three double quotes, not four.  Weird,
  174.     # I know.
  175.     $$self{LQUOTE} =~ s/\"/\"\"/;
  176.     $$self{RQUOTE} =~ s/\"/\"\"/;
  177. }
  178.  
  179. # Initialize the page title information and indentation from our arguments.
  180. sub init_page {
  181.     my ($self) = @_;
  182.  
  183.     # We used to try first to get the version number from a local binary, but
  184.     # we shouldn't need that any more.  Get the version from the running Perl.
  185.     # Work a little magic to handle subversions correctly under both the
  186.     # pre-5.6 and the post-5.6 version numbering schemes.
  187.     my @version = ($] =~ /^(\d+)\.(\d{3})(\d{0,3})$/);
  188.     $version[2] ||= 0;
  189.     $version[2] *= 10 ** (3 - length $version[2]);
  190.     for (@version) { $_ += 0 }
  191.     my $version = join ('.', @version);
  192.  
  193.     # Set the defaults for page titles and indentation if the user didn't
  194.     # override anything.
  195.     $$self{center} = 'User Contributed Perl Documentation'
  196.         unless defined $$self{center};
  197.     $$self{release} = 'perl v' . $version
  198.         unless defined $$self{release};
  199.     $$self{indent} = 4
  200.         unless defined $$self{indent};
  201.  
  202.     # Double quotes in things that will be quoted.
  203.     for (qw/center release/) {
  204.         $$self{$_} =~ s/\"/\"\"/g if $$self{$_};
  205.     }
  206. }
  207.  
  208. ##############################################################################
  209. # Core parsing
  210. ##############################################################################
  211.  
  212. # This is the glue that connects the code below with Pod::Simple itself.  The
  213. # goal is to convert the event stream coming from the POD parser into method
  214. # calls to handlers once the complete content of a tag has been seen.  Each
  215. # paragraph or POD command will have textual content associated with it, and
  216. # as soon as all of a paragraph or POD command has been seen, that content
  217. # will be passed in to the corresponding method for handling that type of
  218. # object.  The exceptions are handlers for lists, which have opening tag
  219. # handlers and closing tag handlers that will be called right away.
  220. #
  221. # The internal hash key PENDING is used to store the contents of a tag until
  222. # all of it has been seen.  It holds a stack of open tags, each one
  223. # represented by a tuple of the attributes hash for the tag, formatting
  224. # options for the tag (which are inherited), and the contents of the tag.
  225.  
  226. # Add a block of text to the contents of the current node, formatting it
  227. # according to the current formatting instructions as we do.
  228. sub _handle_text {
  229.     my ($self, $text) = @_;
  230.     DEBUG > 3 and print "== $text\n";
  231.     my $tag = $$self{PENDING}[-1];
  232.     $$tag[2] .= $self->format_text ($$tag[1], $text);
  233. }
  234.  
  235. # Given an element name, get the corresponding method name.
  236. sub method_for_element {
  237.     my ($self, $element) = @_;
  238.     $element =~ tr/-/_/;
  239.     $element =~ tr/A-Z/a-z/;
  240.     $element =~ tr/_a-z0-9//cd;
  241.     return $element;
  242. }
  243.  
  244. # Handle the start of a new element.  If cmd_element is defined, assume that
  245. # we need to collect the entire tree for this element before passing it to the
  246. # element method, and create a new tree into which we'll collect blocks of
  247. # text and nested elements.  Otherwise, if start_element is defined, call it.
  248. sub _handle_element_start {
  249.     my ($self, $element, $attrs) = @_;
  250.     DEBUG > 3 and print "++ $element (<", join ('> <', %$attrs), ">)\n";
  251.     my $method = $self->method_for_element ($element);
  252.  
  253.     # If we have a command handler, we need to accumulate the contents of the
  254.     # tag before calling it.  Turn off IN_NAME for any command other than
  255.     # <Para> so that IN_NAME isn't still set for the first heading after the
  256.     # NAME heading.
  257.     if ($self->can ("cmd_$method")) {
  258.         DEBUG > 2 and print "<$element> starts saving a tag\n";
  259.         $$self{IN_NAME} = 0 if ($element ne 'Para');
  260.  
  261.         # How we're going to format embedded text blocks depends on the tag
  262.         # and also depends on our parent tags.  Thankfully, inside tags that
  263.         # turn off guesswork and reformatting, nothing else can turn it back
  264.         # on, so this can be strictly inherited.
  265.         my $formatting = $$self{PENDING}[-1][1];
  266.         $formatting = $self->formatting ($formatting, $element);
  267.         push (@{ $$self{PENDING} }, [ $attrs, $formatting, '' ]);
  268.         DEBUG > 4 and print "Pending: [", pretty ($$self{PENDING}), "]\n";
  269.     } elsif ($self->can ("start_$method")) {
  270.         my $method = 'start_' . $method;
  271.         $self->$method ($attrs, '');
  272.     } else {
  273.         DEBUG > 2 and print "No $method start method, skipping\n";
  274.     }
  275. }
  276.  
  277. # Handle the end of an element.  If we had a cmd_ method for this element,
  278. # this is where we pass along the tree that we built.  Otherwise, if we have
  279. # an end_ method for the element, call that.
  280. sub _handle_element_end {
  281.     my ($self, $element) = @_;
  282.     DEBUG > 3 and print "-- $element\n";
  283.     my $method = $self->method_for_element ($element);
  284.  
  285.     # If we have a command handler, pull off the pending text and pass it to
  286.     # the handler along with the saved attribute hash.
  287.     if ($self->can ("cmd_$method")) {
  288.         DEBUG > 2 and print "</$element> stops saving a tag\n";
  289.         my $tag = pop @{ $$self{PENDING} };
  290.         DEBUG > 4 and print "Popped: [", pretty ($tag), "]\n";
  291.         DEBUG > 4 and print "Pending: [", pretty ($$self{PENDING}), "]\n";
  292.         my $method = 'cmd_' . $method;
  293.         my $text = $self->$method ($$tag[0], $$tag[2]);
  294.         if (defined $text) {
  295.             if (@{ $$self{PENDING} } > 1) {
  296.                 $$self{PENDING}[-1][2] .= $text;
  297.             } else {
  298.                 $self->output ($text);
  299.             }
  300.         }
  301.     } elsif ($self->can ("end_$method")) {
  302.         my $method = 'end_' . $method;
  303.         $self->$method ();
  304.     } else {
  305.         DEBUG > 2 and print "No $method end method, skipping\n";
  306.     }
  307. }
  308.  
  309. ##############################################################################
  310. # General formatting
  311. ##############################################################################
  312.  
  313. # Return formatting instructions for a new block.  Takes the current
  314. # formatting and the new element.  Formatting inherits negatively, in the
  315. # sense that if the parent has turned off guesswork, all child elements should
  316. # leave it off.  We therefore return a copy of the same formatting
  317. # instructions but possibly with more things turned off depending on the
  318. # element.
  319. sub formatting {
  320.     my ($self, $current, $element) = @_;
  321.     my %options;
  322.     if ($current) {
  323.         %options = %$current;
  324.     } else {
  325.         %options = (guesswork => 1, cleanup => 1, convert => 1);
  326.     }
  327.     if ($element eq 'Data') {
  328.         $options{guesswork} = 0;
  329.         $options{cleanup} = 0;
  330.         $options{convert} = 0;
  331.     } elsif ($element eq 'X') {
  332.         $options{guesswork} = 0;
  333.         $options{cleanup} = 0;
  334.     } elsif ($element eq 'Verbatim' || $element eq 'C') {
  335.         $options{guesswork} = 0;
  336.         $options{literal} = 1;
  337.     }
  338.     return \%options;
  339. }
  340.  
  341. # Format a text block.  Takes a hash of formatting options and the text to
  342. # format.  Currently, the only formatting options are guesswork, cleanup, and
  343. # convert, all of which are boolean.
  344. sub format_text {
  345.     my ($self, $options, $text) = @_;
  346.     my $guesswork = $$options{guesswork} && !$$self{IN_NAME};
  347.     my $cleanup = $$options{cleanup};
  348.     my $convert = $$options{convert};
  349.     my $literal = $$options{literal};
  350.  
  351.     # Cleanup just tidies up a few things, telling *roff that the hyphens are
  352.     # hard, putting a bit of space between consecutive underscores, and
  353.     # escaping backslashes.  Be careful not to mangle our character
  354.     # translations by doing this before processing character translation.
  355.     if ($cleanup) {
  356.         $text =~ s/\\/\\e/g;
  357.         $text =~ s/-/\\-/g;
  358.         $text =~ s/_(?=_)/_\\|/g;
  359.     }
  360.  
  361.     # Normally we do character translation, but we won't even do that in
  362.     # <Data> blocks or if UTF-8 output is desired.
  363.     if ($convert && !$$self{utf8} && ASCII) {
  364.         $text =~ s/([^\x00-\x7F])/$ESCAPES{ord ($1)} || "X"/eg;
  365.     }
  366.  
  367.     # Ensure that *roff doesn't convert literal quotes to UTF-8 single quotes,
  368.     # but don't mess up our accept escapes.
  369.     if ($literal) {
  370.         $text =~ s/(?<!\\\*)\'/\\*\(Aq/g;
  371.         $text =~ s/(?<!\\\*)\`/\\\`/g;
  372.     }
  373.  
  374.     # If guesswork is asked for, do that.  This involves more substantial
  375.     # formatting based on various heuristics that may only be appropriate for
  376.     # particular documents.
  377.     if ($guesswork) {
  378.         $text = $self->guesswork ($text);
  379.     }
  380.  
  381.     return $text;
  382. }
  383.  
  384. # Handles C<> text, deciding whether to put \*C` around it or not.  This is a
  385. # whole bunch of messy heuristics to try to avoid overquoting, originally from
  386. # Barrie Slaymaker.  This largely duplicates similar code in Pod::Text.
  387. sub quote_literal {
  388.     my $self = shift;
  389.     local $_ = shift;
  390.  
  391.     # A regex that matches the portion of a variable reference that's the
  392.     # array or hash index, separated out just because we want to use it in
  393.     # several places in the following regex.
  394.     my $index = '(?: \[.*\] | \{.*\} )?';
  395.  
  396.     # Check for things that we don't want to quote, and if we find any of
  397.     # them, return the string with just a font change and no quoting.
  398.     m{
  399.       ^\s*
  400.       (?:
  401.          ( [\'\`\"] ) .* \1                             # already quoted
  402.        | \\\*\(Aq .* \\\*\(Aq                           # quoted and escaped
  403.        | \\?\` .* ( \' | \\\*\(Aq )                     # `quoted'
  404.        | \$+ [\#^]? \S $index                           # special ($^Foo, $")
  405.        | [\$\@%&*]+ \#? [:\'\w]+ $index                 # plain var or func
  406.        | [\$\@%&*]* [:\'\w]+ (?: -> )? \(\s*[^\s,]\s*\) # 0/1-arg func call
  407.        | [-+]? ( \d[\d.]* | \.\d+ ) (?: [eE][-+]?\d+ )? # a number
  408.        | 0x [a-fA-F\d]+                                 # a hex constant
  409.       )
  410.       \s*\z
  411.      }xso and return '\f(FS' . $_ . '\f(FE';
  412.  
  413.     # If we didn't return, go ahead and quote the text.
  414.     return '\f(FS\*(C`' . $_ . "\\*(C'\\f(FE";
  415. }
  416.  
  417. # Takes a text block to perform guesswork on.  Returns the text block with
  418. # formatting codes added.  This is the code that marks up various Perl
  419. # constructs and things commonly used in man pages without requiring the user
  420. # to add any explicit markup, and is applied to all non-literal text.  We're
  421. # guaranteed that the text we're applying guesswork to does not contain any
  422. # *roff formatting codes.  Note that the inserted font sequences must be
  423. # treated later with mapfonts or textmapfonts.
  424. #
  425. # This method is very fragile, both in the regular expressions it uses and in
  426. # the ordering of those modifications.  Care and testing is required when
  427. # modifying it.
  428. sub guesswork {
  429.     my $self = shift;
  430.     local $_ = shift;
  431.     DEBUG > 5 and print "   Guesswork called on [$_]\n";
  432.  
  433.     # By the time we reach this point, all hypens will be escaped by adding a
  434.     # backslash.  We want to undo that escaping if they're part of regular
  435.     # words and there's only a single dash, since that's a real hyphen that
  436.     # *roff gets to consider a possible break point.  Make sure that a dash
  437.     # after the first character of a word stays non-breaking, however.
  438.     #
  439.     # Note that this is not user-controllable; we pretty much have to do this
  440.     # transformation or *roff will mangle the output in unacceptable ways.
  441.     s{
  442.         ( (?:\G|^|\s) [\(\"]* [a-zA-Z] ) ( \\- )?
  443.         ( (?: [a-zA-Z\']+ \\-)+ )
  444.         ( [a-zA-Z\']+ ) (?= [\)\".?!,;:]* (?:\s|\Z|\\\ ) )
  445.         \b
  446.     } {
  447.         my ($prefix, $hyphen, $main, $suffix) = ($1, $2, $3, $4);
  448.         $hyphen ||= '';
  449.         $main =~ s/\\-/-/g;
  450.         $prefix . $hyphen . $main . $suffix;
  451.     }egx;
  452.  
  453.     # Translate "--" into a real em-dash if it's used like one.  This means
  454.     # that it's either surrounded by whitespace, it follows a regular word, or
  455.     # it occurs between two regular words.
  456.     if ($$self{MAGIC_EMDASH}) {
  457.         s{          (\s) \\-\\- (\s)                } { $1 . '\*(--' . $2 }egx;
  458.         s{ (\b[a-zA-Z]+) \\-\\- (\s|\Z|[a-zA-Z]+\b) } { $1 . '\*(--' . $2 }egx;
  459.     }
  460.  
  461.     # Make words in all-caps a little bit smaller; they look better that way.
  462.     # However, we don't want to change Perl code (like @ARGV), nor do we want
  463.     # to fix the MIME in MIME-Version since it looks weird with the
  464.     # full-height V.
  465.     #
  466.     # We change only a string of all caps (2) either at the beginning of the
  467.     # line or following regular punctuation (like quotes) or whitespace (1),
  468.     # and followed by either similar punctuation, an em-dash, or the end of
  469.     # the line (3).
  470.     if ($$self{MAGIC_SMALLCAPS}) {
  471.         s{
  472.             ( ^ | [\s\(\"\'\`\[\{<>] | \\\  )                   # (1)
  473.             ( [A-Z] [A-Z] (?: [/A-Z+:\d_\$&] | \\- )* )         # (2)
  474.             (?= [\s>\}\]\(\)\'\".?!,;] | \\*\(-- | \\\  | $ )   # (3)
  475.         } {
  476.             $1 . '\s-1' . $2 . '\s0'
  477.         }egx;
  478.     }
  479.  
  480.     # Note that from this point forward, we have to adjust for \s-1 and \s-0
  481.     # strings inserted around things that we've made small-caps if later
  482.     # transforms should work on those strings.
  483.  
  484.     # Italize functions in the form func(), including functions that are in
  485.     # all capitals, but don't italize if there's anything between the parens.
  486.     # The function must start with an alphabetic character or underscore and
  487.     # then consist of word characters or colons.
  488.     if ($$self{MAGIC_FUNC}) {
  489.         s{
  490.             ( \b | \\s-1 )
  491.             ( [A-Za-z_] ([:\w] | \\s-?[01])+ \(\) )
  492.         } {
  493.             $1 . '\f(IS' . $2 . '\f(IE'
  494.         }egx;
  495.     }
  496.  
  497.     # Change references to manual pages to put the page name in italics but
  498.     # the number in the regular font, with a thin space between the name and
  499.     # the number.  Only recognize func(n) where func starts with an alphabetic
  500.     # character or underscore and contains only word characters, periods (for
  501.     # configuration file man pages), or colons, and n is a single digit,
  502.     # optionally followed by some number of lowercase letters.  Note that this
  503.     # does not recognize man page references like perl(l) or socket(3SOCKET).
  504.     if ($$self{MAGIC_MANREF}) {
  505.         s{
  506.             ( \b | \\s-1 )
  507.             ( [A-Za-z_] (?:[.:\w] | \\- | \\s-?[01])+ )
  508.             ( \( \d [a-z]* \) )
  509.         } {
  510.             $1 . '\f(IS' . $2 . '\f(IE\|' . $3
  511.         }egx;
  512.     }
  513.  
  514.     # Convert simple Perl variable references to a fixed-width font.  Be
  515.     # careful not to convert functions, though; there are too many subtleties
  516.     # with them to want to perform this transformation.
  517.     if ($$self{MAGIC_VARS}) {
  518.         s{
  519.            ( ^ | \s+ )
  520.            ( [\$\@%] [\w:]+ )
  521.            (?! \( )
  522.         } {
  523.             $1 . '\f(FS' . $2 . '\f(FE'
  524.         }egx;
  525.     }
  526.  
  527.     # Fix up double quotes.  Unfortunately, we miss this transformation if the
  528.     # quoted text contains any code with formatting codes and there's not much
  529.     # we can effectively do about that, which makes it somewhat unclear if
  530.     # this is really a good idea.
  531.     s{ \" ([^\"]+) \" } { '\*(L"' . $1 . '\*(R"' }egx;
  532.  
  533.     # Make C++ into \*(C+, which is a squinched version.
  534.     if ($$self{MAGIC_CPP}) {
  535.         s{ \b C\+\+ } {\\*\(C+}gx;
  536.     }
  537.  
  538.     # Done.
  539.     DEBUG > 5 and print "   Guesswork returning [$_]\n";
  540.     return $_;
  541. }
  542.  
  543. ##############################################################################
  544. # Output
  545. ##############################################################################
  546.  
  547. # When building up the *roff code, we don't use real *roff fonts.  Instead, we
  548. # embed font codes of the form \f(<font>[SE] where <font> is one of B, I, or
  549. # F, S stands for start, and E stands for end.  This method turns these into
  550. # the right start and end codes.
  551. #
  552. # We add this level of complexity because the old pod2man didn't get code like
  553. # B<someI<thing> else> right; after I<> it switched back to normal text rather
  554. # than bold.  We take care of this by using variables that state whether bold,
  555. # italic, or fixed are turned on as a combined pointer to our current font
  556. # sequence, and set each to the number of current nestings of start tags for
  557. # that font.
  558. #
  559. # \fP changes to the previous font, but only one previous font is kept.  We
  560. # don't know what the outside level font is; normally it's R, but if we're
  561. # inside a heading it could be something else.  So arrange things so that the
  562. # outside font is always the "previous" font and end with \fP instead of \fR.
  563. # Idea from Zack Weinberg.
  564. sub mapfonts {
  565.     my ($self, $text) = @_;
  566.     my ($fixed, $bold, $italic) = (0, 0, 0);
  567.     my %magic = (F => \$fixed, B => \$bold, I => \$italic);
  568.     my $last = '\fR';
  569.     $text =~ s<
  570.         \\f\((.)(.)
  571.     > <
  572.         my $sequence = '';
  573.         my $f;
  574.         if ($last ne '\fR') { $sequence = '\fP' }
  575.         ${ $magic{$1} } += ($2 eq 'S') ? 1 : -1;
  576.         $f = $$self{FONTS}{ ($fixed && 1) . ($bold && 1) . ($italic && 1) };
  577.         if ($f eq $last) {
  578.             '';
  579.         } else {
  580.             if ($f ne '\fR') { $sequence .= $f }
  581.             $last = $f;
  582.             $sequence;
  583.         }
  584.     >gxe;
  585.     return $text;
  586. }
  587.  
  588. # Unfortunately, there is a bug in Solaris 2.6 nroff (not present in GNU
  589. # groff) where the sequence \fB\fP\f(CW\fP leaves the font set to B rather
  590. # than R, presumably because \f(CW doesn't actually do a font change.  To work
  591. # around this, use a separate textmapfonts for text blocks where the default
  592. # font is always R and only use the smart mapfonts for headings.
  593. sub textmapfonts {
  594.     my ($self, $text) = @_;
  595.     my ($fixed, $bold, $italic) = (0, 0, 0);
  596.     my %magic = (F => \$fixed, B => \$bold, I => \$italic);
  597.     $text =~ s<
  598.         \\f\((.)(.)
  599.     > <
  600.         ${ $magic{$1} } += ($2 eq 'S') ? 1 : -1;
  601.         $$self{FONTS}{ ($fixed && 1) . ($bold && 1) . ($italic && 1) };
  602.     >gxe;
  603.     return $text;
  604. }
  605.  
  606. # Given a command and a single argument that may or may not contain double
  607. # quotes, handle double-quote formatting for it.  If there are no double
  608. # quotes, just return the command followed by the argument in double quotes.
  609. # If there are double quotes, use an if statement to test for nroff, and for
  610. # nroff output the command followed by the argument in double quotes with
  611. # embedded double quotes doubled.  For other formatters, remap paired double
  612. # quotes to LQUOTE and RQUOTE.
  613. sub switchquotes {
  614.     my ($self, $command, $text, $extra) = @_;
  615.     $text =~ s/\\\*\([LR]\"/\"/g;
  616.  
  617.     # We also have to deal with \*C` and \*C', which are used to add the
  618.     # quotes around C<> text, since they may expand to " and if they do this
  619.     # confuses the .SH macros and the like no end.  Expand them ourselves.
  620.     # Also separate troff from nroff if there are any fixed-width fonts in use
  621.     # to work around problems with Solaris nroff.
  622.     my $c_is_quote = ($$self{LQUOTE} =~ /\"/) || ($$self{RQUOTE} =~ /\"/);
  623.     my $fixedpat = join '|', @{ $$self{FONTS} }{'100', '101', '110', '111'};
  624.     $fixedpat =~ s/\\/\\\\/g;
  625.     $fixedpat =~ s/\(/\\\(/g;
  626.     if ($text =~ m/\"/ || $text =~ m/$fixedpat/) {
  627.         $text =~ s/\"/\"\"/g;
  628.         my $nroff = $text;
  629.         my $troff = $text;
  630.         $troff =~ s/\"\"([^\"]*)\"\"/\`\`$1\'\'/g;
  631.         if ($c_is_quote and $text =~ m/\\\*\(C[\'\`]/) {
  632.             $nroff =~ s/\\\*\(C\`/$$self{LQUOTE}/g;
  633.             $nroff =~ s/\\\*\(C\'/$$self{RQUOTE}/g;
  634.             $troff =~ s/\\\*\(C[\'\`]//g;
  635.         }
  636.         $nroff = qq("$nroff") . ($extra ? " $extra" : '');
  637.         $troff = qq("$troff") . ($extra ? " $extra" : '');
  638.  
  639.         # Work around the Solaris nroff bug where \f(CW\fP leaves the font set
  640.         # to Roman rather than the actual previous font when used in headings.
  641.         # troff output may still be broken, but at least we can fix nroff by
  642.         # just switching the font changes to the non-fixed versions.
  643.         $nroff =~ s/\Q$$self{FONTS}{100}\E(.*?)\\f[PR]/$1/g;
  644.         $nroff =~ s/\Q$$self{FONTS}{101}\E(.*?)\\f([PR])/\\fI$1\\f$2/g;
  645.         $nroff =~ s/\Q$$self{FONTS}{110}\E(.*?)\\f([PR])/\\fB$1\\f$2/g;
  646.         $nroff =~ s/\Q$$self{FONTS}{111}\E(.*?)\\f([PR])/\\f\(BI$1\\f$2/g;
  647.  
  648.         # Now finally output the command.  Bother with .ie only if the nroff
  649.         # and troff output aren't the same.
  650.         if ($nroff ne $troff) {
  651.             return ".ie n $command $nroff\n.el $command $troff\n";
  652.         } else {
  653.             return "$command $nroff\n";
  654.         }
  655.     } else {
  656.         $text = qq("$text") . ($extra ? " $extra" : '');
  657.         return "$command $text\n";
  658.     }
  659. }
  660.  
  661. # Protect leading quotes and periods against interpretation as commands.  Also
  662. # protect anything starting with a backslash, since it could expand or hide
  663. # something that *roff would interpret as a command.  This is overkill, but
  664. # it's much simpler than trying to parse *roff here.
  665. sub protect {
  666.     my ($self, $text) = @_;
  667.     $text =~ s/^([.\'\\])/\\&$1/mg;
  668.     return $text;
  669. }
  670.  
  671. # Make vertical whitespace if NEEDSPACE is set, appropriate to the indentation
  672. # level the situation.  This function is needed since in *roff one has to
  673. # create vertical whitespace after paragraphs and between some things, but
  674. # other macros create their own whitespace.  Also close out a sequence of
  675. # repeated =items, since calling makespace means we're about to begin the item
  676. # body.
  677. sub makespace {
  678.     my ($self) = @_;
  679.     $self->output (".PD\n") if $$self{ITEMS} > 1;
  680.     $$self{ITEMS} = 0;
  681.     $self->output ($$self{INDENT} > 0 ? ".Sp\n" : ".PP\n")
  682.         if $$self{NEEDSPACE};
  683. }
  684.  
  685. # Output any pending index entries, and optionally an index entry given as an
  686. # argument.  Support multiple index entries in X<> separated by slashes, and
  687. # strip special escapes from index entries.
  688. sub outindex {
  689.     my ($self, $section, $index) = @_;
  690.     my @entries = map { split m%\s*/\s*% } @{ $$self{INDEX} };
  691.     return unless ($section || @entries);
  692.  
  693.     # We're about to output all pending entries, so clear our pending queue.
  694.     $$self{INDEX} = [];
  695.  
  696.     # Build the output.  Regular index entries are marked Xref, and headings
  697.     # pass in their own section.  Undo some *roff formatting on headings.
  698.     my @output;
  699.     if (@entries) {
  700.         push @output, [ 'Xref', join (' ', @entries) ];
  701.     }
  702.     if ($section) {
  703.         $index =~ s/\\-/-/g;
  704.         $index =~ s/\\(?:s-?\d|.\(..|.)//g;
  705.         push @output, [ $section, $index ];
  706.     }
  707.  
  708.     # Print out the .IX commands.
  709.     for (@output) {
  710.         my ($type, $entry) = @$_;
  711.         $entry =~ s/\"/\"\"/g;
  712.         $self->output (".IX $type " . '"' . $entry . '"' . "\n");
  713.     }
  714. }
  715.  
  716. # Output some text, without any additional changes.
  717. sub output {
  718.     my ($self, @text) = @_;
  719.     print { $$self{output_fh} } @text;
  720. }
  721.  
  722. ##############################################################################
  723. # Document initialization
  724. ##############################################################################
  725.  
  726. # Handle the start of the document.  Here we handle empty documents, as well
  727. # as setting up our basic macros in a preamble and building the page title.
  728. sub start_document {
  729.     my ($self, $attrs) = @_;
  730.     if ($$attrs{contentless} && !$$self{ALWAYS_EMIT_SOMETHING}) {
  731.         DEBUG and print "Document is contentless\n";
  732.         $$self{CONTENTLESS} = 1;
  733.         return;
  734.     }
  735.  
  736.     # If we were given the utf8 option, set an output encoding on our file
  737.     # handle.  Wrap in an eval in case we're using a version of Perl too old
  738.     # to understand this.
  739.     #
  740.     # This is evil because it changes the global state of a file handle that
  741.     # we may not own.  However, we can't just blindly encode all output, since
  742.     # there may be a pre-applied output encoding (such as from PERL_UNICODE)
  743.     # and then we would double-encode.  This seems to be the least bad
  744.     # approach.
  745.     if ($$self{utf8}) {
  746.         eval { binmode ($$self{output_fh}, ':encoding(UTF-8)') };
  747.     }
  748.  
  749.     # Determine information for the preamble and then output it.
  750.     my ($name, $section);
  751.     if (defined $$self{name}) {
  752.         $name = $$self{name};
  753.         $section = $$self{section} || 1;
  754.     } else {
  755.         ($name, $section) = $self->devise_title;
  756.     }
  757.     my $date = $$self{date} || $self->devise_date;
  758.     $self->preamble ($name, $section, $date)
  759.         unless $self->bare_output or DEBUG > 9;
  760.  
  761.     # Initialize a few per-document variables.
  762.     $$self{INDENT}    = 0;      # Current indentation level.
  763.     $$self{INDENTS}   = [];     # Stack of indentations.
  764.     $$self{INDEX}     = [];     # Index keys waiting to be printed.
  765.     $$self{IN_NAME}   = 0;      # Whether processing the NAME section.
  766.     $$self{ITEMS}     = 0;      # The number of consecutive =items.
  767.     $$self{ITEMTYPES} = [];     # Stack of =item types, one per list.
  768.     $$self{SHIFTWAIT} = 0;      # Whether there is a shift waiting.
  769.     $$self{SHIFTS}    = [];     # Stack of .RS shifts.
  770.     $$self{PENDING}   = [[]];   # Pending output.
  771. }
  772.  
  773. # Handle the end of the document.  This does nothing but print out a final
  774. # comment at the end of the document under debugging.
  775. sub end_document {
  776.     my ($self) = @_;
  777.     return if $self->bare_output;
  778.     return if ($$self{CONTENTLESS} && !$$self{ALWAYS_EMIT_SOMETHING});
  779.     $self->output (q(.\" [End document]) . "\n") if DEBUG;
  780. }
  781.  
  782. # Try to figure out the name and section from the file name and return them as
  783. # a list, returning an empty name and section 1 if we can't find any better
  784. # information.  Uses File::Basename and File::Spec as necessary.
  785. sub devise_title {
  786.     my ($self) = @_;
  787.     my $name = $self->source_filename || '';
  788.     my $section = $$self{section} || 1;
  789.     $section = 3 if (!$$self{section} && $name =~ /\.pm\z/i);
  790.     $name =~ s/\.p(od|[lm])\z//i;
  791.  
  792.     # If the section isn't 3, then the name defaults to just the basename of
  793.     # the file.  Otherwise, assume we're dealing with a module.  We want to
  794.     # figure out the full module name from the path to the file, but we don't
  795.     # want to include too much of the path into the module name.  Lose
  796.     # anything up to the first off:
  797.     #
  798.     #     */lib/*perl*/         standard or site_perl module
  799.     #     */*perl*/lib/         from -Dprefix=/opt/perl
  800.     #     */*perl*/             random module hierarchy
  801.     #
  802.     # which works.  Also strip off a leading site, site_perl, or vendor_perl
  803.     # component, any OS-specific component, and any version number component,
  804.     # and strip off an initial component of "lib" or "blib/lib" since that's
  805.     # what ExtUtils::MakeMaker creates.  splitdir requires at least File::Spec
  806.     # 0.8.
  807.     if ($section !~ /^3/) {
  808.         require File::Basename;
  809.         $name = uc File::Basename::basename ($name);
  810.     } else {
  811.         require File::Spec;
  812.         my ($volume, $dirs, $file) = File::Spec->splitpath ($name);
  813.         my @dirs = File::Spec->splitdir ($dirs);
  814.         my $cut = 0;
  815.         my $i;
  816.         for ($i = 0; $i < @dirs; $i++) {
  817.             if ($dirs[$i] =~ /perl/) {
  818.                 $cut = $i + 1;
  819.                 $cut++ if ($dirs[$i + 1] && $dirs[$i + 1] eq 'lib');
  820.                 last;
  821.             }
  822.         }
  823.         if ($cut > 0) {
  824.             splice (@dirs, 0, $cut);
  825.             shift @dirs if ($dirs[0] =~ /^(site|vendor)(_perl)?$/);
  826.             shift @dirs if ($dirs[0] =~ /^[\d.]+$/);
  827.             shift @dirs if ($dirs[0] =~ /^(.*-$^O|$^O-.*|$^O)$/);
  828.         }
  829.         shift @dirs if $dirs[0] eq 'lib';
  830.         splice (@dirs, 0, 2) if ($dirs[0] eq 'blib' && $dirs[1] eq 'lib');
  831.  
  832.         # Remove empty directories when building the module name; they
  833.         # occur too easily on Unix by doubling slashes.
  834.         $name = join ('::', (grep { $_ ? $_ : () } @dirs), $file);
  835.     }
  836.     return ($name, $section);
  837. }
  838.  
  839. # Determine the modification date and return that, properly formatted in ISO
  840. # format.  If we can't get the modification date of the input, instead use the
  841. # current time.  Pod::Simple returns a completely unuseful stringified file
  842. # handle as the source_filename for input from a file handle, so we have to
  843. # deal with that as well.
  844. sub devise_date {
  845.     my ($self) = @_;
  846.     my $input = $self->source_filename;
  847.     my $time;
  848.     if ($input) {
  849.         $time = (stat $input)[9] || time;
  850.     } else {
  851.         $time = time;
  852.     }
  853.     return strftime ('%Y-%m-%d', localtime $time);
  854. }
  855.  
  856. # Print out the preamble and the title.  The meaning of the arguments to .TH
  857. # unfortunately vary by system; some systems consider the fourth argument to
  858. # be a "source" and others use it as a version number.  Generally it's just
  859. # presented as the left-side footer, though, so it doesn't matter too much if
  860. # a particular system gives it another interpretation.
  861. #
  862. # The order of date and release used to be reversed in older versions of this
  863. # module, but this order is correct for both Solaris and Linux.
  864. sub preamble {
  865.     my ($self, $name, $section, $date) = @_;
  866.     my $preamble = $self->preamble_template (!$$self{utf8});
  867.  
  868.     # Build the index line and make sure that it will be syntactically valid.
  869.     my $index = "$name $section";
  870.     $index =~ s/\"/\"\"/g;
  871.  
  872.     # If name or section contain spaces, quote them (section really never
  873.     # should, but we may as well be cautious).
  874.     for ($name, $section) {
  875.         if (/\s/) {
  876.             s/\"/\"\"/g;
  877.             $_ = '"' . $_ . '"';
  878.         }
  879.     }
  880.  
  881.     # Double quotes in date, since it will be quoted.
  882.     $date =~ s/\"/\"\"/g;
  883.  
  884.     # Substitute into the preamble the configuration options.
  885.     $preamble =~ s/\@CFONT\@/$$self{fixed}/;
  886.     $preamble =~ s/\@LQUOTE\@/$$self{LQUOTE}/;
  887.     $preamble =~ s/\@RQUOTE\@/$$self{RQUOTE}/;
  888.     chomp $preamble;
  889.  
  890.     # Get the version information.
  891.     my $version = $self->version_report;
  892.  
  893.     # Finally output everything.
  894.     $self->output (<<"----END OF HEADER----");
  895. .\\" Automatically generated by $version
  896. .\\"
  897. .\\" Standard preamble:
  898. .\\" ========================================================================
  899. $preamble
  900. .\\" ========================================================================
  901. .\\"
  902. .IX Title "$index"
  903. .TH $name $section "$date" "$$self{release}" "$$self{center}"
  904. .\\" For nroff, turn off justification.  Always turn off hyphenation; it makes
  905. .\\" way too many mistakes in technical documents.
  906. .if n .ad l
  907. .nh
  908. ----END OF HEADER----
  909.     $self->output (".\\\" [End of preamble]\n") if DEBUG;
  910. }
  911.  
  912. ##############################################################################
  913. # Text blocks
  914. ##############################################################################
  915.  
  916. # Handle a basic block of text.  The only tricky part of this is if this is
  917. # the first paragraph of text after an =over, in which case we have to change
  918. # indentations for *roff.
  919. sub cmd_para {
  920.     my ($self, $attrs, $text) = @_;
  921.     my $line = $$attrs{start_line};
  922.  
  923.     # Output the paragraph.  We also have to handle =over without =item.  If
  924.     # there's an =over without =item, SHIFTWAIT will be set, and we need to
  925.     # handle creation of the indent here.  Add the shift to SHIFTS so that it
  926.     # will be cleaned up on =back.
  927.     $self->makespace;
  928.     if ($$self{SHIFTWAIT}) {
  929.         $self->output (".RS $$self{INDENT}\n");
  930.         push (@{ $$self{SHIFTS} }, $$self{INDENT});
  931.         $$self{SHIFTWAIT} = 0;
  932.     }
  933.  
  934.     # Add the line number for debugging, but not in the NAME section just in
  935.     # case the comment would confuse apropos.
  936.     $self->output (".\\\" [At source line $line]\n")
  937.         if defined ($line) && DEBUG && !$$self{IN_NAME};
  938.  
  939.     # Force exactly one newline at the end and strip unwanted trailing
  940.     # whitespace at the end.
  941.     $text =~ s/\s*$/\n/;
  942.  
  943.     # Output the paragraph.
  944.     $self->output ($self->protect ($self->textmapfonts ($text)));
  945.     $self->outindex;
  946.     $$self{NEEDSPACE} = 1;
  947.     return '';
  948. }
  949.  
  950. # Handle a verbatim paragraph.  Put a null token at the beginning of each line
  951. # to protect against commands and wrap in .Vb/.Ve (which we define in our
  952. # prelude).
  953. sub cmd_verbatim {
  954.     my ($self, $attrs, $text) = @_;
  955.  
  956.     # Ignore an empty verbatim paragraph.
  957.     return unless $text =~ /\S/;
  958.  
  959.     # Force exactly one newline at the end and strip unwanted trailing
  960.     # whitespace at the end.
  961.     $text =~ s/\s*$/\n/;
  962.  
  963.     # Get a count of the number of lines before the first blank line, which
  964.     # we'll pass to .Vb as its parameter.  This tells *roff to keep that many
  965.     # lines together.  We don't want to tell *roff to keep huge blocks
  966.     # together.
  967.     my @lines = split (/\n/, $text);
  968.     my $unbroken = 0;
  969.     for (@lines) {
  970.         last if /^\s*$/;
  971.         $unbroken++;
  972.     }
  973.     $unbroken = 10 if ($unbroken > 12 && !$$self{MAGIC_VNOPAGEBREAK_LIMIT});
  974.  
  975.     # Prepend a null token to each line.
  976.     $text =~ s/^/\\&/gm;
  977.  
  978.     # Output the results.
  979.     $self->makespace;
  980.     $self->output (".Vb $unbroken\n$text.Ve\n");
  981.     $$self{NEEDSPACE} = 1;
  982.     return '';
  983. }
  984.  
  985. # Handle literal text (produced by =for and similar constructs).  Just output
  986. # it with the minimum of changes.
  987. sub cmd_data {
  988.     my ($self, $attrs, $text) = @_;
  989.     $text =~ s/^\n+//;
  990.     $text =~ s/\n{0,2}$/\n/;
  991.     $self->output ($text);
  992.     return '';
  993. }
  994.  
  995. ##############################################################################
  996. # Headings
  997. ##############################################################################
  998.  
  999. # Common code for all headings.  This is called before the actual heading is
  1000. # output.  It returns the cleaned up heading text (putting the heading all on
  1001. # one line) and may do other things, like closing bad =item blocks.
  1002. sub heading_common {
  1003.     my ($self, $text, $line) = @_;
  1004.     $text =~ s/\s+$//;
  1005.     $text =~ s/\s*\n\s*/ /g;
  1006.  
  1007.     # This should never happen; it means that we have a heading after =item
  1008.     # without an intervening =back.  But just in case, handle it anyway.
  1009.     if ($$self{ITEMS} > 1) {
  1010.         $$self{ITEMS} = 0;
  1011.         $self->output (".PD\n");
  1012.     }
  1013.  
  1014.     # Output the current source line.
  1015.     $self->output ( ".\\\" [At source line $line]\n" )
  1016.         if defined ($line) && DEBUG;
  1017.     return $text;
  1018. }
  1019.  
  1020. # First level heading.  We can't output .IX in the NAME section due to a bug
  1021. # in some versions of catman, so don't output a .IX for that section.  .SH
  1022. # already uses small caps, so remove \s0 and \s-1.  Maintain IN_NAME as
  1023. # appropriate.
  1024. sub cmd_head1 {
  1025.     my ($self, $attrs, $text) = @_;
  1026.     $text =~ s/\\s-?\d//g;
  1027.     $text = $self->heading_common ($text, $$attrs{start_line});
  1028.     my $isname = ($text eq 'NAME' || $text =~ /\(NAME\)/);
  1029.     $self->output ($self->switchquotes ('.SH', $self->mapfonts ($text)));
  1030.     $self->outindex ('Header', $text) unless $isname;
  1031.     $$self{NEEDSPACE} = 0;
  1032.     $$self{IN_NAME} = $isname;
  1033.     return '';
  1034. }
  1035.  
  1036. # Second level heading.
  1037. sub cmd_head2 {
  1038.     my ($self, $attrs, $text) = @_;
  1039.     $text = $self->heading_common ($text, $$attrs{start_line});
  1040.     $self->output ($self->switchquotes ('.SS', $self->mapfonts ($text)));
  1041.     $self->outindex ('Subsection', $text);
  1042.     $$self{NEEDSPACE} = 0;
  1043.     return '';
  1044. }
  1045.  
  1046. # Third level heading.  *roff doesn't have this concept, so just put the
  1047. # heading in italics as a normal paragraph.
  1048. sub cmd_head3 {
  1049.     my ($self, $attrs, $text) = @_;
  1050.     $text = $self->heading_common ($text, $$attrs{start_line});
  1051.     $self->makespace;
  1052.     $self->output ($self->textmapfonts ('\f(IS' . $text . '\f(IE') . "\n");
  1053.     $self->outindex ('Subsection', $text);
  1054.     $$self{NEEDSPACE} = 1;
  1055.     return '';
  1056. }
  1057.  
  1058. # Fourth level heading.  *roff doesn't have this concept, so just put the
  1059. # heading as a normal paragraph.
  1060. sub cmd_head4 {
  1061.     my ($self, $attrs, $text) = @_;
  1062.     $text = $self->heading_common ($text, $$attrs{start_line});
  1063.     $self->makespace;
  1064.     $self->output ($self->textmapfonts ($text) . "\n");
  1065.     $self->outindex ('Subsection', $text);
  1066.     $$self{NEEDSPACE} = 1;
  1067.     return '';
  1068. }
  1069.  
  1070. ##############################################################################
  1071. # Formatting codes
  1072. ##############################################################################
  1073.  
  1074. # All of the formatting codes that aren't handled internally by the parser,
  1075. # other than L<> and X<>.
  1076. sub cmd_b { return '\f(BS' . $_[2] . '\f(BE' }
  1077. sub cmd_i { return '\f(IS' . $_[2] . '\f(IE' }
  1078. sub cmd_f { return '\f(IS' . $_[2] . '\f(IE' }
  1079. sub cmd_c { return $_[0]->quote_literal ($_[2]) }
  1080.  
  1081. # Index entries are just added to the pending entries.
  1082. sub cmd_x {
  1083.     my ($self, $attrs, $text) = @_;
  1084.     push (@{ $$self{INDEX} }, $text);
  1085.     return '';
  1086. }
  1087.  
  1088. # Links reduce to the text that we're given, wrapped in angle brackets if it's
  1089. # a URL.
  1090. sub cmd_l {
  1091.     my ($self, $attrs, $text) = @_;
  1092.     return $$attrs{type} eq 'url' ? "<$text>" : $text;
  1093. }
  1094.  
  1095. ##############################################################################
  1096. # List handling
  1097. ##############################################################################
  1098.  
  1099. # Handle the beginning of an =over block.  Takes the type of the block as the
  1100. # first argument, and then the attr hash.  This is called by the handlers for
  1101. # the four different types of lists (bullet, number, text, and block).
  1102. sub over_common_start {
  1103.     my ($self, $type, $attrs) = @_;
  1104.     my $line = $$attrs{start_line};
  1105.     my $indent = $$attrs{indent};
  1106.     DEBUG > 3 and print " Starting =over $type (line $line, indent ",
  1107.         ($indent || '?'), "\n";
  1108.  
  1109.     # Find the indentation level.
  1110.     unless (defined ($indent) && $indent =~ /^[-+]?\d{1,4}\s*$/) {
  1111.         $indent = $$self{indent};
  1112.     }
  1113.  
  1114.     # If we've gotten multiple indentations in a row, we need to emit the
  1115.     # pending indentation for the last level that we saw and haven't acted on
  1116.     # yet.  SHIFTS is the stack of indentations that we've actually emitted
  1117.     # code for.
  1118.     if (@{ $$self{SHIFTS} } < @{ $$self{INDENTS} }) {
  1119.         $self->output (".RS $$self{INDENT}\n");
  1120.         push (@{ $$self{SHIFTS} }, $$self{INDENT});
  1121.     }
  1122.  
  1123.     # Now, do record-keeping.  INDENTS is a stack of indentations that we've
  1124.     # seen so far, and INDENT is the current level of indentation.  ITEMTYPES
  1125.     # is a stack of list types that we've seen.
  1126.     push (@{ $$self{INDENTS} }, $$self{INDENT});
  1127.     push (@{ $$self{ITEMTYPES} }, $type);
  1128.     $$self{INDENT} = $indent + 0;
  1129.     $$self{SHIFTWAIT} = 1;
  1130. }
  1131.  
  1132. # End an =over block.  Takes no options other than the class pointer.
  1133. # Normally, once we close a block and therefore remove something from INDENTS,
  1134. # INDENTS will now be longer than SHIFTS, indicating that we also need to emit
  1135. # *roff code to close the indent.  This isn't *always* true, depending on the
  1136. # circumstance.  If we're still inside an indentation, we need to emit another
  1137. # .RE and then a new .RS to unconfuse *roff.
  1138. sub over_common_end {
  1139.     my ($self) = @_;
  1140.     DEBUG > 3 and print " Ending =over\n";
  1141.     $$self{INDENT} = pop @{ $$self{INDENTS} };
  1142.     pop @{ $$self{ITEMTYPES} };
  1143.  
  1144.     # If we emitted code for that indentation, end it.
  1145.     if (@{ $$self{SHIFTS} } > @{ $$self{INDENTS} }) {
  1146.         $self->output (".RE\n");
  1147.         pop @{ $$self{SHIFTS} };
  1148.     }
  1149.  
  1150.     # If we're still in an indentation, *roff will have now lost track of the
  1151.     # right depth of that indentation, so fix that.
  1152.     if (@{ $$self{INDENTS} } > 0) {
  1153.         $self->output (".RE\n");
  1154.         $self->output (".RS $$self{INDENT}\n");
  1155.     }
  1156.     $$self{NEEDSPACE} = 1;
  1157.     $$self{SHIFTWAIT} = 0;
  1158. }
  1159.  
  1160. # Dispatch the start and end calls as appropriate.
  1161. sub start_over_bullet { my $s = shift; $s->over_common_start ('bullet', @_) }
  1162. sub start_over_number { my $s = shift; $s->over_common_start ('number', @_) }
  1163. sub start_over_text   { my $s = shift; $s->over_common_start ('text',   @_) }
  1164. sub start_over_block  { my $s = shift; $s->over_common_start ('block',  @_) }
  1165. sub end_over_bullet { $_[0]->over_common_end }
  1166. sub end_over_number { $_[0]->over_common_end }
  1167. sub end_over_text   { $_[0]->over_common_end }
  1168. sub end_over_block  { $_[0]->over_common_end }
  1169.  
  1170. # The common handler for all item commands.  Takes the type of the item, the
  1171. # attributes, and then the text of the item.
  1172. #
  1173. # Emit an index entry for anything that's interesting, but don't emit index
  1174. # entries for things like bullets and numbers.  Newlines in an item title are
  1175. # turned into spaces since *roff can't handle them embedded.
  1176. sub item_common {
  1177.     my ($self, $type, $attrs, $text) = @_;
  1178.     my $line = $$attrs{start_line};
  1179.     DEBUG > 3 and print "  $type item (line $line): $text\n";
  1180.  
  1181.     # Clean up the text.  We want to end up with two variables, one ($text)
  1182.     # which contains any body text after taking out the item portion, and
  1183.     # another ($item) which contains the actual item text.
  1184.     $text =~ s/\s+$//;
  1185.     my ($item, $index);
  1186.     if ($type eq 'bullet') {
  1187.         $item = "\\\(bu";
  1188.         $text =~ s/\n*$/\n/;
  1189.     } elsif ($type eq 'number') {
  1190.         $item = $$attrs{number} . '.';
  1191.     } else {
  1192.         $item = $text;
  1193.         $item =~ s/\s*\n\s*/ /g;
  1194.         $text = '';
  1195.         $index = $item if ($item =~ /\w/);
  1196.     }
  1197.  
  1198.     # Take care of the indentation.  If shifts and indents are equal, close
  1199.     # the top shift, since we're about to create an indentation with .IP.
  1200.     # Also output .PD 0 to turn off spacing between items if this item is
  1201.     # directly following another one.  We only have to do that once for a
  1202.     # whole chain of items so do it for the second item in the change.  Note
  1203.     # that makespace is what undoes this.
  1204.     if (@{ $$self{SHIFTS} } == @{ $$self{INDENTS} }) {
  1205.         $self->output (".RE\n");
  1206.         pop @{ $$self{SHIFTS} };
  1207.     }
  1208.     $self->output (".PD 0\n") if ($$self{ITEMS} == 1);
  1209.  
  1210.     # Now, output the item tag itself.
  1211.     $item = $self->textmapfonts ($item);
  1212.     $self->output ($self->switchquotes ('.IP', $item, $$self{INDENT}));
  1213.     $$self{NEEDSPACE} = 0;
  1214.     $$self{ITEMS}++;
  1215.     $$self{SHIFTWAIT} = 0;
  1216.  
  1217.     # If body text for this item was included, go ahead and output that now.
  1218.     if ($text) {
  1219.         $text =~ s/\s*$/\n/;
  1220.         $self->makespace;
  1221.         $self->output ($self->protect ($self->textmapfonts ($text)));
  1222.         $$self{NEEDSPACE} = 1;
  1223.     }
  1224.     $self->outindex ($index ? ('Item', $index) : ());
  1225. }
  1226.  
  1227. # Dispatch the item commands to the appropriate place.
  1228. sub cmd_item_bullet { my $self = shift; $self->item_common ('bullet', @_) }
  1229. sub cmd_item_number { my $self = shift; $self->item_common ('number', @_) }
  1230. sub cmd_item_text   { my $self = shift; $self->item_common ('text',   @_) }
  1231. sub cmd_item_block  { my $self = shift; $self->item_common ('block',  @_) }
  1232.  
  1233. ##############################################################################
  1234. # Backward compatibility
  1235. ##############################################################################
  1236.  
  1237. # Reset the underlying Pod::Simple object between calls to parse_from_file so
  1238. # that the same object can be reused to convert multiple pages.
  1239. sub parse_from_file {
  1240.     my $self = shift;
  1241.     $self->reinit;
  1242.  
  1243.     # Fake the old cutting option to Pod::Parser.  This fiddings with internal
  1244.     # Pod::Simple state and is quite ugly; we need a better approach.
  1245.     if (ref ($_[0]) eq 'HASH') {
  1246.         my $opts = shift @_;
  1247.         if (defined ($$opts{-cutting}) && !$$opts{-cutting}) {
  1248.             $$self{in_pod} = 1;
  1249.             $$self{last_was_blank} = 1;
  1250.         }
  1251.     }
  1252.  
  1253.     # Do the work.
  1254.     my $retval = $self->SUPER::parse_from_file (@_);
  1255.  
  1256.     # Flush output, since Pod::Simple doesn't do this.  Ideally we should also
  1257.     # close the file descriptor if we had to open one, but we can't easily
  1258.     # figure this out.
  1259.     my $fh = $self->output_fh ();
  1260.     my $oldfh = select $fh;
  1261.     my $oldflush = $|;
  1262.     $| = 1;
  1263.     print $fh '';
  1264.     $| = $oldflush;
  1265.     select $oldfh;
  1266.     return $retval;
  1267. }
  1268.  
  1269. # Pod::Simple failed to provide this backward compatibility function, so
  1270. # implement it ourselves.  File handles are one of the inputs that
  1271. # parse_from_file supports.
  1272. sub parse_from_filehandle {
  1273.     my $self = shift;
  1274.     $self->parse_from_file (@_);
  1275. }
  1276.  
  1277. ##############################################################################
  1278. # Translation tables
  1279. ##############################################################################
  1280.  
  1281. # The following table is adapted from Tom Christiansen's pod2man.  It assumes
  1282. # that the standard preamble has already been printed, since that's what
  1283. # defines all of the accent marks.  We really want to do something better than
  1284. # this when *roff actually supports other character sets itself, since these
  1285. # results are pretty poor.
  1286. #
  1287. # This only works in an ASCII world.  What to do in a non-ASCII world is very
  1288. # unclear -- hopefully we can assume UTF-8 and just leave well enough alone.
  1289. @ESCAPES{0xA0 .. 0xFF} = (
  1290.     "\\ ", undef, undef, undef,            undef, undef, undef, undef,
  1291.     undef, undef, undef, undef,            undef, "\\%", undef, undef,
  1292.  
  1293.     undef, undef, undef, undef,            undef, undef, undef, undef,
  1294.     undef, undef, undef, undef,            undef, undef, undef, undef,
  1295.  
  1296.     "A\\*`",  "A\\*'", "A\\*^", "A\\*~",   "A\\*:", "A\\*o", "\\*(AE", "C\\*,",
  1297.     "E\\*`",  "E\\*'", "E\\*^", "E\\*:",   "I\\*`", "I\\*'", "I\\*^",  "I\\*:",
  1298.  
  1299.     "\\*(D-", "N\\*~", "O\\*`", "O\\*'",   "O\\*^", "O\\*~", "O\\*:",  undef,
  1300.     "O\\*/",  "U\\*`", "U\\*'", "U\\*^",   "U\\*:", "Y\\*'", "\\*(Th", "\\*8",
  1301.  
  1302.     "a\\*`",  "a\\*'", "a\\*^", "a\\*~",   "a\\*:", "a\\*o", "\\*(ae", "c\\*,",
  1303.     "e\\*`",  "e\\*'", "e\\*^", "e\\*:",   "i\\*`", "i\\*'", "i\\*^",  "i\\*:",
  1304.  
  1305.     "\\*(d-", "n\\*~", "o\\*`", "o\\*'",   "o\\*^", "o\\*~", "o\\*:",  undef,
  1306.     "o\\*/" , "u\\*`", "u\\*'", "u\\*^",   "u\\*:", "y\\*'", "\\*(th", "y\\*:",
  1307. ) if ASCII;
  1308.  
  1309. ##############################################################################
  1310. # Premable
  1311. ##############################################################################
  1312.  
  1313. # The following is the static preamble which starts all *roff output we
  1314. # generate.  Most is static except for the font to use as a fixed-width font,
  1315. # which is designed by @CFONT@, and the left and right quotes to use for C<>
  1316. # text, designated by @LQOUTE@ and @RQUOTE@.  However, the second part, which
  1317. # defines the accent marks, is only used if $escapes is set to true.
  1318. sub preamble_template {
  1319.     my ($self, $accents) = @_;
  1320.     my $preamble = <<'----END OF PREAMBLE----';
  1321. .de Sp \" Vertical space (when we can't use .PP)
  1322. .if t .sp .5v
  1323. .if n .sp
  1324. ..
  1325. .de Vb \" Begin verbatim text
  1326. .ft @CFONT@
  1327. .nf
  1328. .ne \\$1
  1329. ..
  1330. .de Ve \" End verbatim text
  1331. .ft R
  1332. .fi
  1333. ..
  1334. .\" Set up some character translations and predefined strings.  \*(-- will
  1335. .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
  1336. .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
  1337. .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
  1338. .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
  1339. .\" nothing in troff, for use with C<>.
  1340. .tr \(*W-
  1341. .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
  1342. .ie n \{\
  1343. .    ds -- \(*W-
  1344. .    ds PI pi
  1345. .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
  1346. .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
  1347. .    ds L" ""
  1348. .    ds R" ""
  1349. .    ds C` @LQUOTE@
  1350. .    ds C' @RQUOTE@
  1351. 'br\}
  1352. .el\{\
  1353. .    ds -- \|\(em\|
  1354. .    ds PI \(*p
  1355. .    ds L" ``
  1356. .    ds R" ''
  1357. 'br\}
  1358. .\"
  1359. .\" Escape single quotes in literal strings from groff's Unicode transform.
  1360. .ie \n(.g .ds Aq \(aq
  1361. .el       .ds Aq '
  1362. .\"
  1363. .\" If the F register is turned on, we'll generate index entries on stderr for
  1364. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  1365. .\" entries marked with X<> in POD.  Of course, you'll have to process the
  1366. .\" output yourself in some meaningful fashion.
  1367. .ie \nF \{\
  1368. .    de IX
  1369. .    tm Index:\\$1\t\\n%\t"\\$2"
  1370. ..
  1371. .    nr % 0
  1372. .    rr F
  1373. .\}
  1374. .el \{\
  1375. .    de IX
  1376. ..
  1377. .\}
  1378. ----END OF PREAMBLE----
  1379.  
  1380.     if ($accents) {
  1381.         $preamble .= <<'----END OF PREAMBLE----'
  1382. .\"
  1383. .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
  1384. .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
  1385. .    \" fudge factors for nroff and troff
  1386. .if n \{\
  1387. .    ds #H 0
  1388. .    ds #V .8m
  1389. .    ds #F .3m
  1390. .    ds #[ \f1
  1391. .    ds #] \fP
  1392. .\}
  1393. .if t \{\
  1394. .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
  1395. .    ds #V .6m
  1396. .    ds #F 0
  1397. .    ds #[ \&
  1398. .    ds #] \&
  1399. .\}
  1400. .    \" simple accents for nroff and troff
  1401. .if n \{\
  1402. .    ds ' \&
  1403. .    ds ` \&
  1404. .    ds ^ \&
  1405. .    ds , \&
  1406. .    ds ~ ~
  1407. .    ds /
  1408. .\}
  1409. .if t \{\
  1410. .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
  1411. .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
  1412. .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
  1413. .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
  1414. .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
  1415. .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
  1416. .\}
  1417. .    \" troff and (daisy-wheel) nroff accents
  1418. .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
  1419. .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
  1420. .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
  1421. .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
  1422. .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
  1423. .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
  1424. .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
  1425. .ds ae a\h'-(\w'a'u*4/10)'e
  1426. .ds Ae A\h'-(\w'A'u*4/10)'E
  1427. .    \" corrections for vroff
  1428. .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
  1429. .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
  1430. .    \" for low resolution devices (crt and lpr)
  1431. .if \n(.H>23 .if \n(.V>19 \
  1432. \{\
  1433. .    ds : e
  1434. .    ds 8 ss
  1435. .    ds o a
  1436. .    ds d- d\h'-1'\(ga
  1437. .    ds D- D\h'-1'\(hy
  1438. .    ds th \o'bp'
  1439. .    ds Th \o'LP'
  1440. .    ds ae ae
  1441. .    ds Ae AE
  1442. .\}
  1443. .rm #[ #] #H #V #F C
  1444. ----END OF PREAMBLE----
  1445. #`# for cperl-mode
  1446.     }
  1447.     return $preamble;
  1448. }
  1449.  
  1450. ##############################################################################
  1451. # Module return value and documentation
  1452. ##############################################################################
  1453.  
  1454. 1;
  1455. __END__
  1456.  
  1457. =head1 NAME
  1458.  
  1459. Pod::Man - Convert POD data to formatted *roff input
  1460.  
  1461. =head1 SYNOPSIS
  1462.  
  1463.     use Pod::Man;
  1464.     my $parser = Pod::Man->new (release => $VERSION, section => 8);
  1465.  
  1466.     # Read POD from STDIN and write to STDOUT.
  1467.     $parser->parse_file (\*STDIN);
  1468.  
  1469.     # Read POD from file.pod and write to file.1.
  1470.     $parser->parse_from_file ('file.pod', 'file.1');
  1471.  
  1472. =head1 DESCRIPTION
  1473.  
  1474. Pod::Man is a module to convert documentation in the POD format (the
  1475. preferred language for documenting Perl) into *roff input using the man
  1476. macro set.  The resulting *roff code is suitable for display on a terminal
  1477. using L<nroff(1)>, normally via L<man(1)>, or printing using L<troff(1)>.
  1478. It is conventionally invoked using the driver script B<pod2man>, but it can
  1479. also be used directly.
  1480.  
  1481. As a derived class from Pod::Simple, Pod::Man supports the same methods and
  1482. interfaces.  See L<Pod::Simple> for all the details.
  1483.  
  1484. new() can take options, in the form of key/value pairs that control the
  1485. behavior of the parser.  See below for details.
  1486.  
  1487. If no options are given, Pod::Man uses the name of the input file with any
  1488. trailing C<.pod>, C<.pm>, or C<.pl> stripped as the man page title, to
  1489. section 1 unless the file ended in C<.pm> in which case it defaults to
  1490. section 3, to a centered title of "User Contributed Perl Documentation", to
  1491. a centered footer of the Perl version it is run with, and to a left-hand
  1492. footer of the modification date of its input (or the current date if given
  1493. STDIN for input).
  1494.  
  1495. Pod::Man assumes that your *roff formatters have a fixed-width font named
  1496. CW.  If yours is called something else (like CR), use the C<fixed> option to
  1497. specify it.  This generally only matters for troff output for printing.
  1498. Similarly, you can set the fonts used for bold, italic, and bold italic
  1499. fixed-width output.
  1500.  
  1501. Besides the obvious pod conversions, Pod::Man also takes care of formatting
  1502. func(), func(3), and simple variable references like $foo or @bar so you
  1503. don't have to use code escapes for them; complex expressions like
  1504. C<$fred{'stuff'}> will still need to be escaped, though.  It also translates
  1505. dashes that aren't used as hyphens into en dashes, makes long dashes--like
  1506. this--into proper em dashes, fixes "paired quotes," makes C++ look right,
  1507. puts a little space between double underbars, makes ALLCAPS a teeny bit
  1508. smaller in B<troff>, and escapes stuff that *roff treats as special so that
  1509. you don't have to.
  1510.  
  1511. The recognized options to new() are as follows.  All options take a single
  1512. argument.
  1513.  
  1514. =over 4
  1515.  
  1516. =item center
  1517.  
  1518. Sets the centered page header to use instead of "User Contributed Perl
  1519. Documentation".
  1520.  
  1521. =item date
  1522.  
  1523. Sets the left-hand footer.  By default, the modification date of the input
  1524. file will be used, or the current date if stat() can't find that file (the
  1525. case if the input is from STDIN), and the date will be formatted as
  1526. YYYY-MM-DD.
  1527.  
  1528. =item fixed
  1529.  
  1530. The fixed-width font to use for vertabim text and code.  Defaults to CW.
  1531. Some systems may want CR instead.  Only matters for B<troff> output.
  1532.  
  1533. =item fixedbold
  1534.  
  1535. Bold version of the fixed-width font.  Defaults to CB.  Only matters for
  1536. B<troff> output.
  1537.  
  1538. =item fixeditalic
  1539.  
  1540. Italic version of the fixed-width font (actually, something of a misnomer,
  1541. since most fixed-width fonts only have an oblique version, not an italic
  1542. version).  Defaults to CI.  Only matters for B<troff> output.
  1543.  
  1544. =item fixedbolditalic
  1545.  
  1546. Bold italic (probably actually oblique) version of the fixed-width font.
  1547. Pod::Man doesn't assume you have this, and defaults to CB.  Some systems
  1548. (such as Solaris) have this font available as CX.  Only matters for B<troff>
  1549. output.
  1550.  
  1551. =item name
  1552.  
  1553. Set the name of the manual page.  Without this option, the manual name is
  1554. set to the uppercased base name of the file being converted unless the
  1555. manual section is 3, in which case the path is parsed to see if it is a Perl
  1556. module path.  If it is, a path like C<.../lib/Pod/Man.pm> is converted into
  1557. a name like C<Pod::Man>.  This option, if given, overrides any automatic
  1558. determination of the name.
  1559.  
  1560. =item quotes
  1561.  
  1562. Sets the quote marks used to surround CE<lt>> text.  If the value is a
  1563. single character, it is used as both the left and right quote; if it is two
  1564. characters, the first character is used as the left quote and the second as
  1565. the right quoted; and if it is four characters, the first two are used as
  1566. the left quote and the second two as the right quote.
  1567.  
  1568. This may also be set to the special value C<none>, in which case no quote
  1569. marks are added around CE<lt>> text (but the font is still changed for troff
  1570. output).
  1571.  
  1572. =item release
  1573.  
  1574. Set the centered footer.  By default, this is the version of Perl you run
  1575. Pod::Man under.  Note that some system an macro sets assume that the
  1576. centered footer will be a modification date and will prepend something like
  1577. "Last modified: "; if this is the case, you may want to set C<release> to
  1578. the last modified date and C<date> to the version number.
  1579.  
  1580. =item section
  1581.  
  1582. Set the section for the C<.TH> macro.  The standard section numbering
  1583. convention is to use 1 for user commands, 2 for system calls, 3 for
  1584. functions, 4 for devices, 5 for file formats, 6 for games, 7 for
  1585. miscellaneous information, and 8 for administrator commands.  There is a lot
  1586. of variation here, however; some systems (like Solaris) use 4 for file
  1587. formats, 5 for miscellaneous information, and 7 for devices.  Still others
  1588. use 1m instead of 8, or some mix of both.  About the only section numbers
  1589. that are reliably consistent are 1, 2, and 3.
  1590.  
  1591. By default, section 1 will be used unless the file ends in .pm in which case
  1592. section 3 will be selected.
  1593.  
  1594. =item utf8
  1595.  
  1596. By default, Pod::Man produces the most conservative possible *roff output
  1597. to try to ensure that it will work with as many different *roff
  1598. implementations as possible.  Many *roff implementations cannot handle
  1599. non-ASCII characters, so this means all non-ASCII characters are converted
  1600. either to a *roff escape sequence that tries to create a properly accented
  1601. character (at least for troff output) or to C<X>.
  1602.  
  1603. If this option is set, Pod::Man will instead output UTF-8.  If your *roff
  1604. implementation can handle it, this is the best output format to use and
  1605. avoids corruption of documents containing non-ASCII characters.  However,
  1606. be warned that *roff source with literal UTF-8 characters is not supported
  1607. by many implementations and may even result in segfaults and other bad
  1608. behavior.
  1609.  
  1610. Be aware that, when using this option, the input encoding of your POD
  1611. source must be properly declared unless it is US-ASCII or Latin-1.  POD
  1612. input without an C<=encoding> command will be assumed to be in Latin-1,
  1613. and if it's actually in UTF-8, the output will be double-encoded.  See
  1614. L<perlpod(1)> for more information on the C<=encoding> command.
  1615.  
  1616. =back
  1617.  
  1618. The standard Pod::Simple method parse_file() takes one argument naming the
  1619. POD file to read from.  By default, the output is sent to STDOUT, but this
  1620. can be changed with the output_fd() method.
  1621.  
  1622. The standard Pod::Simple method parse_from_file() takes up to two
  1623. arguments, the first being the input file to read POD from and the second
  1624. being the file to write the formatted output to.
  1625.  
  1626. You can also call parse_lines() to parse an array of lines or
  1627. parse_string_document() to parse a document already in memory.  To put the
  1628. output into a string instead of a file handle, call the output_string()
  1629. method.  See L<Pod::Simple> for the specific details.
  1630.  
  1631. =head1 DIAGNOSTICS
  1632.  
  1633. =over 4
  1634.  
  1635. =item roff font should be 1 or 2 chars, not "%s"
  1636.  
  1637. (F) You specified a *roff font (using C<fixed>, C<fixedbold>, etc.) that
  1638. wasn't either one or two characters.  Pod::Man doesn't support *roff fonts
  1639. longer than two characters, although some *roff extensions do (the canonical
  1640. versions of B<nroff> and B<troff> don't either).
  1641.  
  1642. =item Invalid quote specification "%s"
  1643.  
  1644. (F) The quote specification given (the quotes option to the constructor) was
  1645. invalid.  A quote specification must be one, two, or four characters long.
  1646.  
  1647. =back
  1648.  
  1649. =head1 BUGS
  1650.  
  1651. Encoding handling assumes that PerlIO is available and does not work
  1652. properly if it isn't since encode and decode do not work well in
  1653. combination with PerlIO encoding layers.  It's very unclear how to
  1654. correctly handle this without PerlIO encoding layers.  The C<utf8> option
  1655. is therefore not supported unless Perl is built with PerlIO support.
  1656.  
  1657. There is currently no way to turn off the guesswork that tries to format
  1658. unmarked text appropriately, and sometimes it isn't wanted (particularly
  1659. when using POD to document something other than Perl).  Most of the work
  1660. towards fixing this has now been done, however, and all that's still needed
  1661. is a user interface.
  1662.  
  1663. The NAME section should be recognized specially and index entries emitted
  1664. for everything in that section.  This would have to be deferred until the
  1665. next section, since extraneous things in NAME tends to confuse various man
  1666. page processors.  Currently, no index entries are emitted for anything in
  1667. NAME.
  1668.  
  1669. Pod::Man doesn't handle font names longer than two characters.  Neither do
  1670. most B<troff> implementations, but GNU troff does as an extension.  It would
  1671. be nice to support as an option for those who want to use it.
  1672.  
  1673. The preamble added to each output file is rather verbose, and most of it
  1674. is only necessary in the presence of non-ASCII characters.  It would
  1675. ideally be nice if all of those definitions were only output if needed,
  1676. perhaps on the fly as the characters are used.
  1677.  
  1678. Pod::Man is excessively slow.
  1679.  
  1680. =head1 CAVEATS
  1681.  
  1682. If Pod::Man is given the C<utf8> option, the encoding of its output file
  1683. handle will be forced to UTF-8 if possible, overriding any existing
  1684. encoding.  This will be done even if the file handle is not created by
  1685. Pod::Man and was passed in from outside.  This seems to be the only way to
  1686. consistently enforce UTF-8-encoded output regardless of PERL_UNICODE and
  1687. other settings.
  1688.  
  1689. The handling of hyphens and em dashes is somewhat fragile, and one may get
  1690. the wrong one under some circumstances.  This should only matter for
  1691. B<troff> output.
  1692.  
  1693. When and whether to use small caps is somewhat tricky, and Pod::Man doesn't
  1694. necessarily get it right.
  1695.  
  1696. Converting neutral double quotes to properly matched double quotes doesn't
  1697. work unless there are no formatting codes between the quote marks.  This
  1698. only matters for troff output.
  1699.  
  1700. =head1 AUTHOR
  1701.  
  1702. Russ Allbery <rra@stanford.edu>, based I<very> heavily on the original
  1703. B<pod2man> by Tom Christiansen <tchrist@mox.perl.com>.  The modifications to
  1704. work with Pod::Simple instead of Pod::Parser were originally contributed by
  1705. Sean Burke (but I've since hacked them beyond recognition and all bugs are
  1706. mine).
  1707.  
  1708. =head1 COPYRIGHT AND LICENSE
  1709.  
  1710. Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
  1711. by Russ Allbery <rra@stanford.edu>.
  1712.  
  1713. This program is free software; you may redistribute it and/or modify it
  1714. under the same terms as Perl itself.
  1715.  
  1716. =head1 SEE ALSO
  1717.  
  1718. L<Pod::Simple>, L<perlpod(1)>, L<pod2man(1)>, L<nroff(1)>, L<troff(1)>,
  1719. L<man(1)>, L<man(7)>
  1720.  
  1721. Ossanna, Joseph F., and Brian W. Kernighan.  "Troff User's Manual,"
  1722. Computing Science Technical Report No. 54, AT&T Bell Laboratories.  This is
  1723. the best documentation of standard B<nroff> and B<troff>.  At the time of
  1724. this writing, it's available at
  1725. L<http://www.cs.bell-labs.com/cm/cs/cstr.html>.
  1726.  
  1727. The man page documenting the man macro set may be L<man(5)> instead of
  1728. L<man(7)> on your system.  Also, please see L<pod2man(1)> for extensive
  1729. documentation on writing manual pages if you've not done it before and
  1730. aren't familiar with the conventions.
  1731.  
  1732. The current version of this module is always available from its web site at
  1733. L<http://www.eyrie.org/~eagle/software/podlators/>.  It is also part of the
  1734. Perl core distribution as of 5.6.0.
  1735.  
  1736. =cut
  1737.